<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Http\Requests\Shop\UserStore;
use App\Http\Requests\Shop\UserUpdate;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
   /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
   public function index(Request $request)
   {
      $where = function ($query) use ($request) {
         //按昵称手机号搜索
         if ($request->has('keyword') and $request->keyword != '') {
            $cate = $request->cate;
            $search = "%" . $request->keyword . "%";
            $query->where($cate, 'like', $search);
         }
      };

      $users = User::with('orders')->where($where)->paginate(config('admin.page_size'));
      return response()->json($users);
   }

   /**
    * Store a newly created resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @return \Illuminate\Http\Response
    */
   public function store(UserStore $request)
   {
      $user = User::create([
         'name' => $request->name,
         'headimgurl' => $request->headimgurl,
         'sex' => $request->sex,
         'phone' => $request->phone,
         'email' => $request->email,
         'password' => bcrypt($request->password)
      ]);

      if ($user) {
         DB::table('accounts')->insert(['user_id' => $user['id'], 'created_at' => date('Y-m-d H:i:s')]); //新增账户表
         $info = ['status' => 1, 'msg' => '新增会员成功~'];
         return response()->json($info);
      }
   }

   /**
    * Display the specified resource.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function show($id)
   {
      $user = User::find($id);
      return response()->json($user);
   }


   /**
    * Update the specified resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function update(UserUpdate $request, $id)
   {
      $user = User::find($id);

      $info = ['status' => 1, 'msg' => '更新成功~'];
      if ($request->has('password') && $request->password != '') {
         if (!Hash::check($request->oldPass, $user->password)) {
            $info = ['status' => 0, 'msg' => '原始密码错误~'];
         }
      }
      $user->update([
         'name' => $request->name,
         'headimgurl' => $request->headimgurl,
         'sex' => $request->sex,
         'phone' => $request->phone,
         'email' => $request->email,
         'password' => bcrypt($request->password)
      ]);

      return response()->json($info);
   }

   /**
    * Remove the specified resource from storage.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function destroy($id)
   {
      $user = User::destroy($id);
      if ($user) {
         return response()->json(['status' => 1, 'msg' => '删除会员成功~']);
      } else {
         return response()->json(['status' => 0, 'msg' => '删除会员失败~']);
      }
   }
}
